<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class LunboController extends Controller
{
    //Lunbo
    public function add()
    {
    	
    	return view('admin.lunbo.add',['title' =>'轮播图']);
    }

    public function insert(Request $request)
    {
    	//去除_token
    	$data = $request -> except('_token');
            //处理图片
            // getClientOriginalExtension()    extension()
            $extension = $request -> file('a_spic') -> getClientOriginalExtension();
            // echo $extension;
            $filename = mt_rand(1000000,9999999).'.'.$extension;
            $dir = './uploads/avatar/';

            $request -> file('a_spic') -> move($dir,$filename);//storeAs
            $data['a_spic'] = '/uploads/avatar/'.$filename;
            
            $data['a_screateTime'] = date('Y-m-d H:i:s',time()+24*3600);
            $res = DB::table('admin_slide')->insert($data);

            if($res){
                    return  redirect('/admin/lunbo/index') -> with(['info' => '添加成功']);
             }else{
                    return back() -> with(['info' => '添加失败']);
                  }
                     //dd($data);
        }

        public function index(){
       
        $data = DB::table('admin_slide') -> get();
        //dd($data);
            return  view('/admin/lunbo/index',['data'=>$data]);
        }

        public function edit($a_sid){
            $data = DB::table('admin_slide')->where('a_sid',$a_sid)->first();
            return  view('/admin/lunbo/edit',['data'=>$data]);
        }
    
    	public  function update(Request $request){
            $data = $request -> except('_token');
            //处理图片
            // getClientOriginalExtension()    extension()
            $extension = $request -> file('a_spic') -> getClientOriginalExtension();
            // echo $extension;
            $filename = mt_rand(1000000,9999999).'.'.$extension;
            $dir = './uploads/avatar/';

            $request -> file('a_spic') -> move($dir,$filename);//storeAs
            $data['a_spic'] = '/uploads/avatar/'.$filename;
            
            $data['a_screateTime'] = date('Y-m-d H:i:s',time()+24*3600);

            $res = DB::table('admin_slide')->where('a_sid',$data['a_sid'])->update(['a_spic'=>$data['a_spic']]);
            if($res){
                    return  redirect('/admin/lunbo/index') -> with(['info' => '修改成功']);
             }else{
                    return back() -> with(['info' => '修改失败']);
             }
        }	

		public function delete($a_sid){
            $res = DB::table('admin_slide')->where('a_sid',$a_sid)->delete();
            if($res){
                    return  redirect('/admin/lunbo/index') -> with(['info' => '删除成功']);
             }else{
                 return back() -> with(['info' => '删除失败']);
             }
        }    	
            
    
            
  }          
        

            